import cDyPopup from "../../goods/diyFormPopup";
import { Toast } from "vant";
import QRCode from "qrcode";
export default {
  components: { cDyPopup },
  data() {
    return {
      detail: {},
      order: [],
      popup: false,
      withdrawal: false,
      qrcodeShow: false,
      qrcodeImg: "",

      is_form: 0,
      dfData: {
        fields: {}
      },
      form_id: 0,
      form_data_id: 0,
      profitShow: false,

      networkLoading: false,

      cash: 0,
      dividend_id: 0,
      uniacid: '',
      order_ids: ""
    };
  },
  created() {
    console.log(this.$route.query,this.$route.params,'hhhhhh')
    this.uniacid = this.$route.query.i;
    this.getData();
    this.generate();
    if (this.$route.query.is_scan == 1) {
      this.receive();
    }
  },
  methods: {
    getData() {
      $http.get("plugin.consumer-reward.frontend.controllers.payment-jump.index", { pay_id: this.$route.query.pay_id }, "").then(res => {
        if(this.uniacid == '70' || this.uniacid == '8' ){
          this.getPayInfo(this.$route.query.pay_id);
          // if (res.msg != "") Toast(res.msg);
          if (res.result != 1) return;
          this.detail = res.data;
          this.order = res.data.order;
          if (this.detail.popup == 1) this.popup = true;
        }else {
          if (res.msg != "") Toast(res.msg);
          if (res.result != 1) return;
          this.detail = res.data;
          this.order = res.data.order;
          if (this.detail.popup == 1) this.popup = true;
        }
        
       
      });
    },
    // 支付宝支付成功
    getPayInfo(payId){
      $http.get("Guxinyu.get_order_id", { pay_id: payId }, "").then(res => {
        console.log(res,'get_order_id')
        this.order_ids = res.data.order_ids;
        var payType = res.data.pay_type_id;
        if(payType == 2){
          if(this.uniacid == '70'){
            this.paySuccessDoGu();//谷昕雨新
          }else if(this.uniacid == '8'){
            this.paySuccessDoDou();//都来米
          }
          
        }
        
      });
    },
    // 哆来咪--优惠换购次数扣除
    changeBuyKouchu(){
      $http
      .get(
          "Doulm.youhuiNumDec",
          {
            order_id: this.order_ids,
            uid: localStorage.getItem("uid") ? localStorage.getItem("uid") : localStorage.getItem("newUid")
          },
          "..."
        ).then(response => {
          // this.$router.push(this.fun.getUrl("payBack", json));
      });
    },
     // 哆来咪--升级vip
    updateVip(){
      $http
        .get(
            "Doulm.vip",
            {
              order_id: this.order_ids
            },
            "..."
          ).then(response => {
            // this.$router.push(this.fun.getUrl("payBack", json));
        });
    },
    // 都来米--支付宝支付成功回调
    paySuccessDoDou(){
      $http
      .get(
        "Doulm.youhui",
        {
          uid: localStorage.getItem("uid") ? localStorage.getItem("uid") : localStorage.getItem("newUid"),
          order_id: this.order_ids
        },
        "..."
      )
      .then(response => {
        console.log(response,'youhuihuihuihui')
        if(response.result == 1){
          this.changeBuyKouchu()
          this.updateVip()
          if(response.data.youhui_num > 0){
            // this.$router.push(this.fun.getUrl("newDiy"), {page_id:500});
            this.$router.push("/newDiy?page_id=500&i=8");
          }else {
            // this.$router.push(this.fun.getUrl("payBack", json));
          }
        }else {
          Toast(response.msg)
        }
        // this.$router.push(this.fun.getUrl("payBack", json));
      });
    },
    // 谷昕宇新--支付宝支付成功回调
    paySuccessDoGu(){
      console.log( this.order_ids,' this.order_ids')
      $http
          .get(
            "Guxinyu.web_shengji",
            {
              uid: localStorage.getItem("uid") ? localStorage.getItem("uid") : localStorage.getItem("newUid")
            },
            "..."
          )
          .then(response => {});
        $http
          .get(
            "Guxinyu.pay_order",
            {
              uid: localStorage.getItem("uid") ? localStorage.getItem("uid") : localStorage.getItem("newUid"),
              order_id: this.order_ids
            },
            "..."
          )
          .then(response => {
            // this.$router.push(this.fun.getUrl("payBack", json));
          });
    },
    receive() {
      $http.get("plugin.consumer-reward.frontend.controllers.reward.receive-reward", { pay_id: this.$route.query.pay_id }, "").then(res => {
        Toast(res.msg);
        if (res.result != 1) return;
        setTimeout(() => {
          this.popup = false;
          //先判断是否开启提现，在判断是否授权
          if (this.detail.is_withdraw == 1) {
            if (this.detail.is_bind == 1) {
              this.withdrawal = true;
              this.dfData.fields = res.data.fields;
              this.form_id = res.data.form_id;
              this.networkLoading = true;
              this.is_form = res.data.is_form;
              this.cash = res.data.cash;
              this.dividend_id = res.data.dividend_id;
            } else {
              this.qrcodeShow = true;
            }
          } else {
            this.$router.push(this.fun.getUrl("member", {}));
          }
        }, 1000);
      });
    },
    handWithBtn() {
      // if(this.is_form == 1){
      //   this.profitShow = true;
      // }else {
      this.dfsave();
      // }
    },
    dfsave(form_data_id = 0) {
      console.log(form_data_id);
      this.form_data_id = form_data_id;
      $http
        .get(
          "plugin.consumer-reward.frontend.controllers.withdraw.withdraw",
          {
            form_id: this.form_id,
            form_data_id,
            dividend_id: this.dividend_id,
            cash: this.cash
          },
          ""
        )
        .then(res => {
          Toast(res.msg);
          if (res.result != 1) return;
          this.withdrawal = false;
          if (form_data_id != 0) this.profitShow = false;
          console.log(res);
        });
    },

    generate() {
      let url = window.location.href + "&is_scan=1";
      console.log(url);
      QRCode.toDataURL(url).then(res => {
        this.qrcodeImg = res;
      });
    }
  }
};
